﻿using System;
using Xenta.Entities;
using Xenta.Utils;

namespace Xenta.Operations
{
    /// <summary>
    /// Creates a catalog item specification.
    /// </summary>
    public class CreateCatalogItemDimensionalSpec : CreateCatalogItemSpec<CatalogItemDimensionalSpecEntity>
    {
        #region Properties

        /// <summary>
        /// Gets or sets the weight.
        /// </summary>
        public decimal Weight
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the width.
        /// </summary>
        public decimal Width
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the length.
        /// </summary>
        public decimal Length
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the height.
        /// </summary>
        public decimal Height
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override Err Validate(IUnitOfWork uow)
        {
            if(Weight < Decimal.Zero)
                return Err.Of<CatalogItemDimensionalSpecEntity>().NotValid("Weight");
            if(Length < Decimal.Zero)
                return Err.Of<CatalogItemDimensionalSpecEntity>().NotValid("Length");
            if(Width < Decimal.Zero)
                return Err.Of<CatalogItemDimensionalSpecEntity>().NotValid("Width");
            if(Height < Decimal.Zero)
                return Err.Of<CatalogItemDimensionalSpecEntity>().NotValid("Height");
            return base.Validate(uow);
        }

        protected override void Construct(IUnitOfWork uow, CatalogItemDimensionalSpecEntity entity)
        {
            base.Construct(uow, entity);

            entity.Weight = Weight;
            entity.Height = Height;
            entity.Length = Length;
            entity.Width = Width;
        }

        #endregion
    }

    /// <summary>
    /// Updates the catalog item specification.
    /// </summary>
    public class UpdateCatalogItemDimensionalSpec : UpdateCatalogItemSpec<CatalogItemDimensionalSpecEntity>
    {
        #region Properties

        /// <summary>
        /// Gets or sets the weight.
        /// </summary>
        public decimal Weight
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the width.
        /// </summary>
        public decimal Width
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the length.
        /// </summary>
        public decimal Length
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the height.
        /// </summary>
        public decimal Height
        {
            get;
            set;
        }

        #endregion

        #region Utilities

        protected override Err Validate(IUnitOfWork uow)
        {
            if(Weight < Decimal.Zero)
                return Err.Of<CatalogItemDimensionalSpecEntity>().NotValid("Weight");
            if(Length < Decimal.Zero)
                return Err.Of<CatalogItemDimensionalSpecEntity>().NotValid("Length");
            if(Width < Decimal.Zero)
                return Err.Of<CatalogItemDimensionalSpecEntity>().NotValid("Width");
            if(Height < Decimal.Zero)
                return Err.Of<CatalogItemDimensionalSpecEntity>().NotValid("Height");
            return base.Validate(uow);
        }

        protected override void Revise(IUnitOfWork uow, CatalogItemDimensionalSpecEntity entity)
        {
            base.Revise(uow, entity);

            entity.Weight = Weight;
            entity.Height = Height;
            entity.Width = Width;
            entity.Length = Length;
        }

        #endregion
    }
}
